package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.bean.ServiceDetail;
import com.ciwong.epaper.util.DateUtils;
import com.ciwong.epaper.util.EConstants;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 我的服务详情列表Adapter
 *
 * @author bin
 * @version ciwong v.1.0 2016/4/12
 * @since ciwong v.1.0
 */
public class MyServicesAdapter extends RecyclerView.Adapter<MyServicesAdapter.MyServicesViewHolder> {
	/**
	 * 上下文对象
	 */
	private Context mContext;

	/**
	 * 我的服务详情列表
	 */
	public List<ServiceDetail> mServiceDetails;

	private EApplication application;


	public static final int VIEWTYPE_DELETEABLE    = 1;
	public static final int VIEWTYPE_DISDELETEABLE = 2;


	private Handler mHanlder = new Handler();

	//private int serviceType;

	//private String areaText, periodText;
	private static final String TAG = "MyServicesAdapter";

	/**
	 * 构造函数
	 *
	 * @param context        上下文对象
	 * @param serviceDetails 我的服务详情列表
	 */
	public MyServicesAdapter(Context context,
	                         List<ServiceDetail> serviceDetails) {
		this.mContext = context;
		// addAll(serviceDetails);
	}


	@Override
	public MyServicesViewHolder onCreateViewHolder(ViewGroup parent,
	                                               int viewType) {
		//View v = View.inflate(parent.getContext(), R.layout.adapter_my_services, null);
		View v = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.adapter_my_services, parent, false);
		return new MyServicesViewHolder(v);
	}
	//private ServiceDetail serviceDetail;


	@Override
	public void onBindViewHolder(final MyServicesViewHolder holder, final int position) {
		ImageView imageView;
		ServiceDetail serviceDetail;
		serviceDetail = mServiceDetails.get(position);
		holder.itemView.setTag(serviceDetail);
		String logoUrl = serviceDetail.getLogoUrl();
		if (serviceDetail.getSalesType()==0){//服务
			holder.myServicesLogo.setVisibility(View.VISIBLE);
			holder.myBookLogo.setVisibility(View.GONE);
			imageView=holder.myServicesLogo;
		}else{
			holder.myServicesLogo.setVisibility(View.GONE);
			holder.myBookLogo.setVisibility(View.VISIBLE);
			imageView=holder.myBookLogo;
		}
		ImageLoader.getInstance().displayImage(
				TextUtils.isEmpty(logoUrl)
						? "drawable://" + R.mipmap.ic_launcher : logoUrl,
				imageView,
				EConstants.getDefAvatarCircleBuilder());
		String name = serviceDetail.getServiceTypeName();
		holder.myServicesName.setText(name);


		 String expireTime = serviceDetail.getExpireTime();
		if (expireTime != null && !"".equals(expireTime)) {
			expireTime = expireTime == null ? ""
					: DateUtils.String2typeStr(serviceDetail.getExpireTime(), DateUtils.DATE_LONG, DateUtils.DATE_SHORT0);
		}
		if(serviceDetail.isVip()){
			holder.my_services_area.setText("VIP");
		}else{
			holder.my_services_area.setText("");
		}

		if(serviceDetail.isBuy()&&!serviceDetail.isbExpired()){ //买过未过期，包括(买断，按月买，免费获取)
			if((serviceDetail.getSign()==1&&serviceDetail.getIsFree()==0 )){  //购买模式 1买断 0按月购买  3免费购买
				holder.levelTv.setText("永久使用");
			}else if(serviceDetail.getSign()==3){//serviceDetail.getSign()==0&&serviceDetail.isVip())
				holder.levelTv.setText("永久使用");
			} else{
				holder.levelTv.setText(expireTime + "到期");
			}

			if(serviceDetail.getSign()==3){
				holder.myServicesExpire.setText("已获取");
				holder.my_services_area.setVisibility(View.GONE);
			}else{
				holder.myServicesExpire.setText("已购买");
				holder.my_services_area.setVisibility(View.VISIBLE);
			}
			holder.levelTv.setVisibility(View.VISIBLE);
			holder.mIvArrow.setBackgroundResource(R.mipmap.btn_icon_green);
		}else if(!serviceDetail.isbExpired()){//buy==false&&未过期
			holder.my_services_area.setText("试用");
			holder.levelTv.setText(expireTime + "到期");
			holder.myServicesExpire.setText("试用中");
			holder.levelTv.setVisibility(View.VISIBLE);
			holder.my_services_area.setVisibility(View.VISIBLE);
		}else{
			holder.levelTv.setVisibility(View.VISIBLE);
			holder.my_services_area.setVisibility(View.GONE);
			holder.levelTv.setText("已于" + expireTime + "过期");
			if(serviceDetail.getIsShow()==1){
				holder.myServicesExpire.setText("已过期");
				holder.mIvArrow.setBackgroundResource(R.mipmap.btn_icon_yellow);
			}else{
				holder.myServicesExpire.setText("已下架");
				holder.mIvArrow.setBackgroundResource(R.mipmap.btn_icon_gray);
			}
		}




 		/*if (serviceDetail.isVip()) {
			if (serviceDetail.getSign() == 3) {//购买模式 1买断 0按月购买  3免费购买
				holder.myServicesExpire.setText("已获取");
			} else {
				holder.myServicesExpire.setText("已购买");
			}
			//            holder.myServicesExpire.setTextColor(Color.parseColor("#d8d8d8"));
			//            holder.myServicesExpire.setBackgroundResource(R.drawable.shape_my_service_expire_white);

		} else if (!serviceDetail.isbExpired()) {
			holder.myServicesExpire.setText("试用中");
			//            holder.myServicesExpire.setTextColor(Color.WHITE);
			//            holder.myServicesExpire.setBackgroundResource(R.drawable.shape_my_service_expire_green);

		} else if (serviceDetail.getIsShow() == 1 && serviceDetail.isbExpired()) {
			holder.myServicesExpire.setText("已过期");
			//            holder.myServicesExpire.setTextColor(Color.WHITE);
			//            holder.myServicesExpire.setBackgroundResource(R.drawable.shape_my_service_expire_orange);
			holder.mIvArrow.setBackgroundResource(R.mipmap.btn_icon_yellow);

		} else {
			holder.myServicesExpire.setText("已下架");
			//            holder.myServicesExpire.setTextColor(Color.parseColor("#d8d8d8"));
			//            holder.myServicesExpire.setBackgroundResource(R.drawable.shape_my_service_expire_white);
			holder.mIvArrow.setBackgroundResource(R.mipmap.btn_icon_gray);
		}
		String expireTime = serviceDetail.getExpireTime();
		if (expireTime != null && !"".equals(expireTime)) {
			expireTime = expireTime == null ? ""
					: DateUtils.String2typeStr(serviceDetail.getExpireTime(), DateUtils.DATE_LONG, DateUtils.DATE_SHORT0);
		}
		// VIP
		if (serviceDetail.isVip()) {
			//买断  并且不是赠送
			if ((serviceDetail.getSign() == 1 || serviceDetail.getSign() == 3 )&& serviceDetail.getIsFree() == 0) {

				holder.levelTv.setVisibility(View.VISIBLE);
				holder.my_services_area.setVisibility(View.VISIBLE);
				holder.my_services_area.setText("VIP");
				holder.levelTv.setText("永久使用");

			} else {//按月购买 或者没有拿到模式的  还有可能是赠送
				holder.levelTv.setVisibility(View.VISIBLE);
				holder.my_services_area.setVisibility(View.VISIBLE);
				holder.my_services_area.setText("VIP");
				holder.levelTv.setText(expireTime + "到期");
			}

		} else if (!serviceDetail.isbExpired()) {//试用
			holder.levelTv.setVisibility(View.VISIBLE);
			holder.my_services_area.setVisibility(View.VISIBLE);
			holder.my_services_area.setText("试用");
			holder.levelTv.setText(expireTime + "到期");
		} else {//过期
			holder.levelTv.setVisibility(View.VISIBLE);
			holder.levelTv.setText("已于" + expireTime + "过期");
			holder.my_services_area.setVisibility(View.GONE);
		}*/


	}


	@Override
	public int getItemCount() {
		return mServiceDetails == null ? 0 : mServiceDetails.size();
	}


	public static class MyServicesViewHolder extends RecyclerView.ViewHolder {
		ImageView myServicesLogo;
		ImageView myBookLogo;

		TextView myServicesName, myServicesExpire, levelTv, my_services_area;

		ImageView mIvArrow;

		public MyServicesViewHolder(View itemView) {
			super(itemView);
			myServicesLogo = (ImageView) itemView
					.findViewById(R.id.my_services_logo);
			myBookLogo = (ImageView) itemView
					.findViewById(R.id.my_book_logo);
			myServicesName = (TextView) itemView
					.findViewById(R.id.my_services_name);
			myServicesExpire = (TextView) itemView
					.findViewById(R.id.my_services_expire);
			levelTv = (TextView) itemView.findViewById(R.id.my_services_level);
			my_services_area = (TextView) itemView.findViewById(R.id.my_services_area);
			mIvArrow = (ImageView) itemView.findViewById(R.id.iv_arrow);
		}
	}


	/**
	 * 清空列表
	 */
	public void clear() {
		if (this.mServiceDetails != null) {
			this.mServiceDetails.clear();
			notifyDataSetChanged();
		}
	}

	/**
	 * 添加我的服务详情列表集合
	 *
	 * @param serviceDetails 我的服务详情列表集合
	 */
	public void addAll(List<ServiceDetail> serviceDetails) {
		if (this.mServiceDetails == null) {
			this.mServiceDetails = new ArrayList<ServiceDetail>();
		}
		if (serviceDetails != null) {
			int positionStart = getItemCount();
			for (ServiceDetail serviceDetail : serviceDetails) {
				if (!serviceDetail.isDeleted()) {
					this.mServiceDetails.add(serviceDetail);
				}
			}
			//getService();
			notifyDataSetChanged();
			//notifyItemRangeChanged(positionStart, getItemCount());
		}
	}

	/**
	 * 获取列表
	 */
	public List<ServiceDetail> getServiceDetails() {
		return mServiceDetails;
	}

	@Override
	public int getItemViewType(int position) {

		ServiceDetail serviceDetail = mServiceDetails.get(position);
		if (serviceDetail.isVip()) {
			return VIEWTYPE_DISDELETEABLE;
		} else if (!serviceDetail.isbExpired()) {
			return VIEWTYPE_DISDELETEABLE;
		} else if (serviceDetail.getIsShow() == 1 && serviceDetail.isbExpired()) {
			return VIEWTYPE_DELETEABLE;
		} else {
			return VIEWTYPE_DELETEABLE;
		}

	}
}